package com.sduept.nwld.kpi.controller;

import com.alibaba.fastjson.JSON;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.kpi.manager.KpiDataManager;
import com.sduept.nwld.kpi.model.KpiData;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.*;
@Named
@ViewScoped
public class KpiDataViewController extends AbstractController{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8694316537220818357L;
	@Autowired
	private KpiDataManager dm;
	
	private List<String> voltageGrades = Arrays.asList(
			"500kV及以上",
			"220kV及以上",
			"110kV",
			"35及66kv",
			"35kV以下",
			"直流及串补系统");
	private List<String> companys = Arrays.asList(
			"直调",
			"广东",
			"广州",
			"深圳",
			"广西",
			"云南",
			"贵州",
			"海南"
	);
	private List<String> times = Arrays.asList(
			"201401",
			"201402",
			"201403",
			"201404",
			"201405",
			"201406",
			"201407",
			"201408",
			"201409",
			"201410",
			"201411",
			"201412"
	);
	private List<String> kpis = Arrays.asList(
			"faultCutTimeAve",
			"fastFaultClearingRatio",
			"protectActTimeAve"
	);
	
	private List<String> selectVolGrades = new ArrayList<String>();
	private List<String> selectCompanys  = new ArrayList<String>();
	private List<String> selectTimes = new ArrayList<String>();
	
	
	private String xAxis;
	private String xAxisValueJson;
	
	private List<String> selectKpis;
	
	private List<KpiData> queryResult = new ArrayList<KpiData>();
	//百分率写到y1
	@SuppressWarnings("rawtypes")
	private Map<String, List> y1AxisMap = new HashMap<>();
	//时间写到y2
	@SuppressWarnings("rawtypes")
	private Map<String, List> y2AxisMap = new HashMap<>();
	private String y1AxisJson;
	private String y2AxisJson;
	
	
	@PostConstruct
	public void init(){
		selectVolGrades = voltageGrades;
		selectCompanys = companys; 
		selectTimes = times;
		xAxis = "time";
		selectKpis = kpis;
		filter();
	}
	
	@SuppressWarnings("rawtypes")
	public void filter(){
		y1AxisMap.clear();
		y2AxisMap.clear();
		
		//显示的指标
		if(selectKpis.contains("faultCutTimeAve")){
			y2AxisMap.put("故障平均切除时间", new ArrayList<>());
		}
		if(selectKpis.contains("fastFaultClearingRatio")){
			y1AxisMap.put("故障快速切除率", new ArrayList<>());
		}
		if(selectKpis.contains("protectActTimeAve")){
			y2AxisMap.put("保护平均动作时间", new ArrayList<>());
		}
		Map<String, List> xAxisValue = new HashMap<>();
		List<KpiData> datas = new ArrayList<KpiData>();
		//x轴如果为时间，那么将同一时间的所有的数据加起来求平均
		if(xAxis.equals("time")){
			xAxisValue.put("时间", selectTimes);
			for(String t : selectTimes){
				datas = dm.findBydimension(selectVolGrades,selectCompanys,Arrays.asList(t));
				setKpiValueMap(datas);
			}
		}
		//x轴如果为公司，那么将同一公司的所有数据加起来求平均
		if(xAxis.equals("company")){
			xAxisValue.put("公司", selectCompanys);
			for(String t : selectCompanys){
				datas = dm.findBydimension(selectVolGrades,Arrays.asList(t),selectTimes);
				setKpiValueMap(datas);
			}
		}
		//x轴如果为电压等级，那么将同一电压等级的所有数据加起来求平均
		if(xAxis.equals("voltageGrade")){
			xAxisValue.put("电压等级", selectVolGrades);
			for(String t : selectVolGrades){
				datas = dm.findBydimension(Arrays.asList(t),selectCompanys,selectTimes);
				setKpiValueMap(datas);
			}
		}
		
		y1AxisJson = JSON.toJSONString(y1AxisMap);
		y2AxisJson = JSON.toJSONString(y2AxisMap);
		xAxisValueJson = JSON.toJSONString(xAxisValue);
	}
	
	@SuppressWarnings("unchecked")
	private void setKpiValueMap(List<KpiData> datas){
		Long faultCutTimeAve = (long) 0;
		Double fastFaultClearingRatio = (double) 0;
		Long protectActTimeAve = (long) 0;
		for(KpiData d : datas){
			faultCutTimeAve += d.getFaultCutTimeAve();
			fastFaultClearingRatio += d.getFastFaultClearingRatio();
			protectActTimeAve += d.getProtectActTimeAve();
		}
		faultCutTimeAve = faultCutTimeAve/datas.size();
		fastFaultClearingRatio = fastFaultClearingRatio/datas.size();
		protectActTimeAve = protectActTimeAve/datas.size();
		if(y2AxisMap.containsKey("故障平均切除时间")){
			y2AxisMap.get("故障平均切除时间").add(faultCutTimeAve);
		}
		if(y1AxisMap.containsKey("故障快速切除率")){
			y1AxisMap.get("故障快速切除率").add(fastFaultClearingRatio);
		}
		if(y2AxisMap.containsKey("保护平均动作时间")){
			y2AxisMap.get("保护平均动作时间").add(protectActTimeAve);
		}
	}
	public void clearFilter(){
		init();
	}
	
	
	public void addData(){
		
		List<String> times = new ArrayList<String>();
		for(int i=0;i<12;i++){
			if(i<9){
				times.add("20140"+(i+1));
			}else if(i>=9){
				times.add("2014"+(i+1));
			}
		}
		
		for(String i:voltageGrades){
			for(String j:companys){
				for(String z:times){
					//快速切除率 98
					//故障平均持续时间 50000
					//保护平均动作时间 20000
					
					Double clearRatio = 98+5*Math.random();
					Long faultCutTime = (long) (48000+2000*Math.random());
					Long protectActTime = (long) (15000 + 5000*Math.random());
					KpiData data = new KpiData();
					data.setCompany(j);
					data.setVoltageGrade(i);
					data.setTime(z);
					data.setFaultCutTimeAve(faultCutTime);
					data.setProtectActTimeAve(protectActTime);
					data.setFastFaultClearingRatio(clearRatio);
			
				}
			}
		}
	}
	

	@SuppressWarnings("rawtypes")
	public Map<String, List> getY1AxisMap() {
		return y1AxisMap;
	}

	@SuppressWarnings("rawtypes")
	public void setY1AxisMap(Map<String, List> y1AxisMap) {
		this.y1AxisMap = y1AxisMap;
	}

	@SuppressWarnings("rawtypes")
	public Map<String, List> getY2AxisMap() {
		return y2AxisMap;
	}

	@SuppressWarnings("rawtypes")
	public void setY2AxisMap(Map<String, List> y2AxisMap) {
		this.y2AxisMap = y2AxisMap;
	}

	public String getY1AxisJson() {
		return y1AxisJson;
	}

	public void setY1AxisJson(String y1AxisJson) {
		this.y1AxisJson = y1AxisJson;
	}

	public String getY2AxisJson() {
		return y2AxisJson;
	}

	public void setY2AxisJson(String y2AxisJson) {
		this.y2AxisJson = y2AxisJson;
	}

	public List<String> getSelectVolGrades() {
		return selectVolGrades;
	}
	public void setSelectVolGrades(List<String> selectVolGrades) {
		this.selectVolGrades = selectVolGrades;
	}
	public List<String> getSelectCompanys() {
		return selectCompanys;
	}
	public void setSelectCompanys(List<String> selectCompanys) {
		this.selectCompanys = selectCompanys;
	}
	
	public List<String> getVoltageGrades() {
		return voltageGrades;
	}

	public void setVoltageGrades(List<String> voltageGrades) {
		this.voltageGrades = voltageGrades;
	}

	public List<String> getCompanys() {
		return companys;
	}

	public void setCompanys(List<String> companys) {
		this.companys = companys;
	}

	public List<String> getTimes() {
		return times;
	}

	public void setTimes(List<String> times) {
		this.times = times;
	}

	public String getxAxis() {
		return xAxis;
	}

	public void setxAxis(String xAxis) {
		this.xAxis = xAxis;
	}

	public List<KpiData> getQueryResult() {
		return queryResult;
	}

	public void setQueryResult(List<KpiData> queryResult) {
		this.queryResult = queryResult;
	}

	public List<String> getSelectTimes() {
		return selectTimes;
	}

	public List<String> getSelectKpis() {
		return selectKpis;
	}

	public void setSelectKpis(List<String> selectKpis) {
		this.selectKpis = selectKpis;
	}

	public void setSelectTimes(List<String> selectTimes) {
		this.selectTimes = selectTimes;
	}

	public List<String> getKpis() {
		return kpis;
	}

	public void setKpis(List<String> kpis) {
		this.kpis = kpis;
	}

	public String getxAxisValueJson() {
		return xAxisValueJson;
	}

	public void setxAxisValueJson(String xAxisValueJson) {
		this.xAxisValueJson = xAxisValueJson;
	}

}
